package com.yc.spring.jdbc.dao;

import com.yc.spring.jdbc.entity.Account;
import com.yc.spring.jdbc.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AccountDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<Account> query(){
        String sql = "select * from bank_account";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
    }
    public Account queryById(long id){
        String sql = "select * from bank_account where id=?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class),id);
    }
    public void insert(long id, String password, double balance){
        String sql="insert into bank_account values(?,?,?)";
        jdbcTemplate.update(sql,id,password,balance);
    }
    public int updateBalanceById(long id, double money){
        String sql="update bank_account set balance=? where id=?";
        double balance=queryById(id).getBalance();
        return jdbcTemplate.update(sql,money+balance,id);
    }
    public Account login(Account account){
        String sql="select * from bank_account where id=? and password=? ";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Account.class),account.getId(),account.getPassword());
    }
}
